﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Combo_Add(Combo model ,int ACID);


[OperationContract]
string Combo_Update(Combo model ,int ACID);


[OperationContract]
string ComboByComboID_Delete(int ComboID,int ACID);


[OperationContract]
Combo ComboByComboID_Get(int ComboID,int ACID );


[OperationContract]
List<Combo> Combo_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加Combo表数据
/// <summary>
/// 添加Combo
/// </summary>
/// <param name="model">Combo实体</param>
/// <returns>空或错误</returns>
public string Combo_Add(Combo model,int ACID)
{
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(model.monthFlag==null )
{
return "『按月核算标志』不能为空！";
}
if(string.IsNullOrEmpty(model.ComboName))
{
return "『套餐名称』不能为空！";
}
if(string.IsNullOrEmpty(model.ComboCode))
{
return "『套餐代码』不能为空！";
}
if(string.IsNullOrEmpty(model.ClassCode))
{
return "『套餐类别编码』不能为空！";
}
if (string.IsNullOrEmpty(model.CardGoodsCode))
{
    return "『卡对应产品编码』不能为空！";
}
if(model.CarrierID==null )
{
return "『运营商ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Combo.Count(p=>p.ComboID==model.ComboID)>0)
{
return "主键已存在！";
}
ctx.Combo.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Combo表数据事物用重构
/// <summary>
/// 添加Combo事物用重构
/// </summary>
/// <param name="model">Combo实体</param>
/// <returns>空或错误</returns>
private string Combo_Add(Combo model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(model.monthFlag==null )
{
return "『按月核算标志』不能为空！";
}
if(string.IsNullOrEmpty(model.ComboName))
{
return "『套餐名称』不能为空！";
}
if(string.IsNullOrEmpty(model.ComboCode))
{
return "『套餐代码』不能为空！";
}
if(string.IsNullOrEmpty(model.ClassCode))
{
return "『套餐类别编码』不能为空！";
}
if (string.IsNullOrEmpty(model.CardGoodsCode))
{
    return "『卡对应产品编码』不能为空！";
}
if(model.CarrierID==null )
{
return "『运营商ID』不能为空！";
}
if(ctx.Combo.Count(p=>p.ComboID==model.ComboID)>0)
{
return "主键已存在！";
}
ctx.Combo.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改Combo表数据
/// <summary>
/// 修改Combo
/// </summary>
/// <param name="model">Combo实体</param>
/// <returns>空或错误</returns>
public string Combo_Update(Combo model,int ACID)
{
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(model.monthFlag==null )
{
return "『按月核算标志』不能为空！";
}
if(string.IsNullOrEmpty(model.ComboName))
{
return "『套餐名称』不能为空！";
}
if(string.IsNullOrEmpty(model.ComboCode))
{
return "『套餐代码』不能为空！";
}
if(string.IsNullOrEmpty(model.ClassCode))
{
return "『套餐类别编码』不能为空！";
}
if (string.IsNullOrEmpty(model.CardGoodsCode))
{
    return "『卡对应产品编码』不能为空！";
}
if(model.CarrierID==null )
{
return "『运营商ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Combo.Count(p=>p.ComboID==model.ComboID)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Combo Combomodel=ctx.Combo.Single(p => p.ComboID==model.ComboID);
if(model.ComboID!=null )
{
Combomodel.ComboID=model.ComboID;
}
if(model.CarrierID!=null )
{
Combomodel.CarrierID=model.CarrierID;
}
if(!string.IsNullOrEmpty(model.ClassCode))
{
Combomodel.ClassCode=model.ClassCode;
}
if(!string.IsNullOrEmpty(model.ComboCode))
{
Combomodel.ComboCode=model.ComboCode;
}
if(!string.IsNullOrEmpty(model.ComboName))
{
Combomodel.ComboName=model.ComboName;
}
if(!string.IsNullOrEmpty(model.Hours))
{
Combomodel.Hours=model.Hours;
}
if(!string.IsNullOrEmpty(model.DataSize))
{
Combomodel.DataSize=model.DataSize;
}
if(model.TCZ!=null )
{
Combomodel.TCZ=model.TCZ;
}
if(model.monthFlag!=null )
{
Combomodel.monthFlag=model.monthFlag;
}
if(!string.IsNullOrEmpty(model.Region))
{
Combomodel.Region=model.Region;
}
if(model.inprice!=null )
{
Combomodel.inprice=model.inprice;
}
if(model.outprice!=null )
{
Combomodel.outprice=model.outprice;
}
if(model.LowPrice!=null )
{
Combomodel.LowPrice=model.LowPrice;
}
if(model.DealerPrice1!=null )
{
Combomodel.DealerPrice1=model.DealerPrice1;
}
if(model.DealerPrice2!=null )
{
Combomodel.DealerPrice2=model.DealerPrice2;
}
if(model.DealerPrice3!=null )
{
Combomodel.DealerPrice3=model.DealerPrice3;
}
if(model.DealerPrice4!=null )
{
Combomodel.DealerPrice4=model.DealerPrice4;
}
if(model.DealerPrice5!=null )
{
Combomodel.DealerPrice5=model.DealerPrice5;
}
if(model.VipPrice1!=null )
{
Combomodel.VipPrice1=model.VipPrice1;
}
if(model.VipPrice2!=null )
{
Combomodel.VipPrice2=model.VipPrice2;
}
if(model.VipPrice3!=null )
{
Combomodel.VipPrice3=model.VipPrice3;
}
if(!string.IsNullOrEmpty(model.Remarks))
{
Combomodel.Remarks=model.Remarks;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除Combo表ByComboID数据
/// <summary>
/// 删除ComboByComboID
/// </summary>
/// <param name="ComboID">套餐ID</param>
/// <returns>空或错误</returns>
public string ComboByComboID_Delete(int ComboID,int ACID)
{
if(ComboID==null )
{
return "『套餐ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Combo.Count(p=>p.ComboID==ComboID)!=1)
{
return null;
}
Combo model=ctx.Combo.Single(p => p.ComboID== ComboID);
ctx.Combo.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Combo表ByComboID数据[事物用重构]
/// <summary>
/// 删除ComboByComboID[事物用重构]
/// </summary>
/// <param name="ComboID">套餐ID</param>
/// <returns>空或错误</returns>
private string ComboByComboID_Delete(int ComboID,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(ctx.Combo.Count(p=>p.ComboID==ComboID)!=1)
{
return null;
}
Combo model=ctx.Combo.Single(p => p.ComboID== ComboID);
ctx.Combo.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取Combo表ByComboID数据
/// <summary>
/// 获取ComboByComboID
/// </summary>
/// <param name="ComboID">套餐ID</param>
/// <returns>当前表</returns>
public Combo ComboByComboID_Get(int ComboID,int ACID)
{
if(ComboID==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Combo.Count(p=>p.ComboID==ComboID)!=1)
{
return null;
}
return ctx.Combo.SingleOrDefault(p=>p.ComboID==ComboID);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取Combo表数据
/// <summary>
/// 获取Combo
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Combo></returns>
public List<Combo> Combo_Get(Expression<Func<Combo, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Combo.ToList();
}
 else 
{
return ctx.Combo.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Combo.Where(predicate).ToList();
}
 else 
{
return ctx.Combo.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取Combo表数据
/// <summary>
/// 获取Combo
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Combo></returns>
public List<Combo> Combo_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return Combo_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取Combo表数据计数
/// <summary>
/// 获取Combo表数据计数
/// </summary>
/// <returns>List<Combo></returns>
public int Combo_GetCount(Expression<Func<Combo, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Combo.Count();

}
 else
{
return ctx.Combo.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Combo分页表数据
/// <summary>
/// 获取Combo分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Combo></returns>
public List<Combo> Combo_Getpage(int pageIndex, int pageSize,Expression<Func<Combo, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Combo.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Combo.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Combo.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Combo.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Combo表数据
/// <summary>
/// 批量修改Combo
/// </summary>
/// <param name="predicate">Expression<Func<Combo, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Combo,Combo>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Combo_batchUpdate(Expression<Func<Combo, bool>> predicate,Expression<Func<Combo, Combo>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Combo.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Combo表数据事物用重构
/// <summary>
/// 批量修改Combo事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Combo, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Combo,Combo>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Combo_batchUpdate(Expression<Func<Combo, bool>> predicate,Expression<Func<Combo, Combo>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Combo.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Combo表数据
/// <summary>
/// 批量删除Combo
/// </summary>
/// <param name="predicate">Expression<Func<Combo, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Combo_batchDel(Expression<Func<Combo, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Combo.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Combo表数据事物用重构
/// <summary>
/// 批量删除Combo事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Combo, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Combo_batchDel(Expression<Func<Combo, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Combo.Delete(predicate);
return ok;
}
#endregion


}
}

